import argparse
import os
from model.net import CLSNetV3, CLSNetV4, CLSNetV5, CLSNetV6, CLSNetV7
# from data.tvm_dataset import TVMDataSetV2
from data.tvm_dataset_arm import TVMDataSetARM
import torch
import datetime

from torch.utils.data import DataLoader
import tqdm
from loguru import logger
import sys
from my_utils.log_util import LogUtil
# import pickle

from my_utils.eval_util import eval_class_and_attr

label_list = [
    'divide', 'abs', 'expand_dims', 'negative', 'upsampling', 'batch_matmul', 'global_max_pool2d', 'subtract',
    'minimum', 'concatenate', 'add', 'relu', 'bias_add', 'global_avg_pool2d', 'exp', 'cast', 'multiply', 'clip',
    'upsampling3d', 'tanh', 'max_pool3d', 'log', 'strided_slice', 'pad', 'avg_pool3d', 'max_pool2d', 'maximum', 'dense',
    'transpose', 'sqrt', 'sigmoid', 'adaptive_avg_pool3d', 'batch_flatten', 'conv3d', 'avg_pool2d', 'softmax',
    'greater', 'leaky_relu', 'adaptive_max_pool3d', 'conv2d'
]


def run(args):
    # Create the results dir
    if not os.path.exists("./results"):
        os.mkdir("./results")

    device = torch.device("cuda:{}".format(args.gpu) if torch.cuda.is_available() else "cpu")
    current_time = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")

    logfile_path = LogUtil.create_dir(args, current_time, os.path.basename(sys.argv[0]))

    # Prepare Data
    train_dataset = TVMDataSetARM(args.data,
                                  train_ratio=args.train_ratio if args.eval == 0 else 0.001,
                                  w2v_model=args.w2v_model)
    test_dataset = TVMDataSetARM(
        args.data,
        train_ratio=args.train_ratio,
        mode='test',
        w2v_model=args.w2v_model,
    )

    train_dataloader = DataLoader(train_dataset, batch_size=1, shuffle=True, pin_memory=False, num_workers=1)
    test_dataloader = DataLoader(test_dataset, batch_size=1, shuffle=True, pin_memory=False, num_workers=1)

    # Model Init
    net = CLSNetV3(in_d=200,
                   out_classes=len(label_list),
                   hiden=args.hsize,
                   num_layers=args.num_layers,
                   has_shape=(args.shape_info == 1),
                   has_value=(args.value_trace == 1),
                   has_weight_info=(args.weight_info == 1))

    if args.model == 'v4':
        net = CLSNetV4(in_d=200,
                       out_classes=len(label_list),
                       hiden=args.hsize,
                       num_layers=args.num_layers,
                       has_shape=(args.shape_info == 1),
                       has_value=(args.value_trace == 1),
                       has_weight_info=(args.weight_info == 1))
    elif args.model == 'v5':
        net = CLSNetV5(in_d=200,
                       out_classes=len(label_list),
                       hiden=args.hsize,
                       num_layers=args.num_layers,
                       has_shape=(args.shape_info == 1),
                       has_value=(args.value_trace == 1),
                       has_weight_info=(args.weight_info == 1))
    elif args.model == 'v6':
        net = CLSNetV6(in_d=200,
                       out_classes=len(label_list),
                       hiden=args.hsize,
                       num_layers=args.num_layers,
                       has_shape=(args.shape_info == 1),
                       has_value=(args.value_trace == 1),
                       has_weight_info=(args.weight_info == 1))
    elif args.model == "v7":
        net = CLSNetV7(in_d=200,
                       out_classes=len(label_list),
                       hiden=args.hsize,
                       num_layers=args.num_layers,
                       has_shape=(args.shape_info == 1),
                       has_value=(args.value_trace == 1),
                       has_weight_info=(args.weight_info == 1))

    if not args.ckpt == "None":
        net.load_state_dict(torch.load(args.ckpt))
    net.to(device)

    # Init Optimizor and Loss
    optimizer = torch.optim.Adam(net.parameters(), lr=args.lr)
    if args.optim == "sgd":
        optimizer = torch.optim.SGD(net.parameters(), lr=args.lr)

    # criterion = torch.nn.MSELoss()

    criterion_ce = torch.nn.CrossEntropyLoss()

    if args.eval == 1:
        net.eval()
        logger.info("Start evaluation ...")
        eval_class_and_attr(net, test_dataloader, device, args, logfile_path)
        exit(0)

    # Training
    for i in range(args.epochs):
        pbar = tqdm.tqdm(total=len(train_dataset), file=open(logfile_path, "a+"))
        pbar.set_description("Epoch:{}, Loss:{}".format(i + 1, 0))
        count = 0
        sum_loss = 0
        net.train()
        for input, target, shape_info, value_data in train_dataloader:
            pbar.update(1)
            if len(input) == 0:
                continue
            pred = net(input, device)
            optimizer.zero_grad()
            loss = criterion_ce(pred[0][:len(label_list)].unsqueeze(0), target[0][0].unsqueeze(0).long().to(device))
            # loss += criterion(pred[0][len(label_list):], target[0][1:].to(device))

            if torch.isnan(loss):
                continue
            loss.backward()
            optimizer.step()

            sum_loss += loss.item()
            count += 1

            if count % 10 == 0:
                pbar.set_description("Epoch:{}, Loss:{}".format(i + 1, sum_loss / count))
        pbar.close()
        logger.info("Epoch:{}, Loss:{}".format(i + 1, sum_loss / count))

        # Test
        if i % args.eval_per_epoch == 0 or i == (args.epochs - 1):
            # Saving the checkpoint
            save_path = "results/{}-({})/checkpoints/".format(current_time, os.path.basename(sys.argv[0]))
            torch.save(net.state_dict(), "{}/model_ckpt_{}.pth".format(save_path, i))
            logger.info("Start evaluation ...")
            eval_class_and_attr(net, test_dataloader, device, args, logfile_path)

    logger.info("Finished!")


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='TVM Reversion')
    parser.add_argument("--epochs", default=200, type=int, help="number of epochs")
    parser.add_argument("--lr", default=1e-4, type=float, help="Learning rate")
    parser.add_argument("--hsize", default=200, type=int, help="hidden size of lstm")
    parser.add_argument("--num_layers", default=2, type=int, help="layers of lstm")
    parser.add_argument("--eval_per_epoch", default=2, type=int, help="Do evaluation at every n epochs")
    parser.add_argument("--ckpt", default="None", type=str, help="Checkpoint file")
    parser.add_argument("--optim", default="adam", type=str, help="Optimizer: [adam, sgd]")
    parser.add_argument("--gpu", default="0", type=str)
    parser.add_argument("--eval", default=0, type=int, help="Skip the training step")
    parser.add_argument("--data", default="tvm_data/save_dir/trace_data_all.pkl", type=str, help="The training data")
    parser.add_argument("--w2v_model", default="word2vec-train-ida-arm.model", type=str, help="word2vec model")
    parser.add_argument("--attention", default=0, type=int)
    parser.add_argument("--shape_info", default=0, type=int)
    parser.add_argument("--num_class", default=9, type=int)
    parser.add_argument("--weight_info", default=0, type=int)
    parser.add_argument("--value_trace", default=0, type=int)
    parser.add_argument("--train_ratio", default=0.7, type=float)
    parser.add_argument("--config_path", default="tvm_data/other_funcs/elf/", type=str)
    parser.add_argument("--model", default="v3", type=str)
    parser.add_argument("--log_level", default="INFO", type=str)
    args = parser.parse_args()
    os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu
    run(args)
